package com.survey.action;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.survey.bean.QuesItem;
import com.survey.bean.QuesItemList;
import com.survey.bean.QuesItemListOption;
import com.survey.util.Constant;
import com.survey.util.FieldUtil;
import com.survey.util.MapUtil;
import com.survey.util.MessageUtil;
import com.survey.util.Page;
import com.survey.util.common.BaseAction;
import com.survey.service.QuesItemListOptionService;
import com.survey.service.QuesItemListService;
import com.survey.service.QuesItemService;

@Controller
@RequestMapping("/sys")
public class QuesItemListAction extends BaseAction {
	private String										actionname	= "题目";
	private String										actionclass	= "QuesItemList";
	@Autowired
	private QuesItemListService				service;
	@Autowired
	private QuesItemService						quesItemService;
	@Autowired
	private QuesItemListOptionService	quesItemListOptionService;

	@RequestMapping(value = "/add2QuesItemList.do")
	public String add2() {
		putRequestValue("list", quesItemService.selectAll(QuesItem.class, "quesId desc"));
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addQuesItemList";
	}

	@RequestMapping(value = "/getQuesItemList.do")
	public String get(int uid) {
		try {
			QuesItemList temp = service.get(uid);
			request.setAttribute("modifybean", temp);

			putRequestValue("list", quesItemListOptionService.selectAll(QuesItemListOption.class,
					MapUtil.createMap("quesItemListId", uid), "id desc"));

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyQuesItemList";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/getQuesItemListOptions.do")
	public String getQuesItemListOptions(int uid) {
		try {
			QuesItemList temp = service.get(uid);
			request.setAttribute("modifybean", temp);

			putRequestValue("list", quesItemListOptionService.selectAll(QuesItemListOption.class,
					MapUtil.createMap("quesItemListId", uid), "id desc"));

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyQuesItemListOptions";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	private static String[] os = new String[] { "非常好", "较好", "一般", "不好" };

	@RequestMapping(value = "/addQuesItemList.do")
	public String add(QuesItemList bean) {
		try {
			service.add(bean);
			log.info(bean.getId());
			//			if ("单选".equals(bean.getTypesel())) {
			//				for (String s : os) {
			//					QuesItemListOption o = new QuesItemListOption();
			//					o.setAmount(0);
			//					o.setQuesItemListId(bean.getId());
			//					o.setOptions(s);
			//					quesItemListOptionService.add(o);
			//					
			//				}
			//			}
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateQuesItemList.do")
	public String update(QuesItemList bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteQuesItemList.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQuesItemList.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, QuesItemList.class, parmnames, parmvalues);

			Page page = service.selectPage(p, QuesItemList.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			putRequestValue("list", quesItemService.selectAll(QuesItem.class, "quesId desc"));

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listQuesItemList";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
